package org.ponly.cta4j.factory;

import java.util.Locale;
import org.ponly.cta4j.Captcha;
import org.ponly.cta4j.CaptchaFactory;

/**
 * 代理验证码工厂.
 * <p>
 * 子类应该实现 {@link #getCaptchaFactory()} 返回一个被代理的 验证码工厂.
 * </p>
 *
 * @author vacoor
 * @since 1.0
 */
public abstract class ProxiedCaptchaFactory implements CaptchaFactory {

    /**
     * {@inheritDoc}
     */
    @Override
    public Captcha getCaptcha() {
        return getRequiredCaptchaFactory().getCaptcha();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Captcha getCaptcha(final Locale locale) {
        return getRequiredCaptchaFactory().getCaptcha(locale);
    }

    /**
     * 获取一个必须的验证码工厂.
     *
     * @return 验证码工厂
     */
    protected final CaptchaFactory getRequiredCaptchaFactory() {
        final CaptchaFactory factory = getCaptchaFactory();
        if (null == factory) {
            throw new IllegalStateException("not invalid captcha factory found.");
        }
        return factory;
    }

    /**
     * @return 被代理的验证码工厂
     */
    protected abstract CaptchaFactory getCaptchaFactory();

}
